******************************************************************************************************************************************************************************* 
* STATA SCRIPT
* 
*             by : Kenneth Bunker (Universidad San Sebastián)
*                : Gabriel L. Negretto (Universidad Carlos III de Madrid)
* corresponding  : kenneth.bunker@uss.cl
* journal        : Party Politics
* article        : The Party System Effects of Unstable Electoral Rules in Latin America
*
* CITE DOI    	 : Bunker, Kenneth; Negretto, Gabriel L., 2023, "Replication 
*                 	Data for: The Party System Effects of Unstable Electoral 
*                 	Rules in Latin America", https://doi.org/10.7910/DVN/QPA1T3, 
*                 	Harvard Dataverse  
*
*   version: Stata/IC 15.1 for Mac (64-bit Intel)
*
******************************************************************************************************************************************************************************* 

clear
set matsize 150

******************************************************************************************************************************************************************************* 
 ****** OPEN DATA
******************************************************************************************************************************************************************************* 

** set working directory:														<------------- SET WORKING DIRECTORY HERE!
cd "/Users/kennethbunker/Dropbox/1. Negretto - Revisiting interactions/2023"

** import data:																	<------------- IMPORT DATA HERE!
import excel "../Data/replication data (HD).xlsx", firstrow clear

******************************************************************************************************************************************************************************* 
 ****** GENERATE VARIABLES
*******************************************************************************************************************************************************************************

gen ethnic				= e2
gen proximity 			= p1
gen maglog 				= ln(dep_dm_1tier)
gen eth_maglog 			= ethnic * maglog
gen eth_number 			= ethnic * number
gen maglog_number 		= maglog * number
gen eth_maglog_number 	= ethnic * maglog * number
gen stability			= dep_elec_acc
gen eth_stability 		= ethnic * stability
gen maglog_stability 	= maglog * stability
gen eth_maglog_stability= ethnic * maglog * stability
gen eth_upper 			= ethnic * upper
gen enep_upper 			= enpv_bn * upper
gen enc_prox 			= enc_bn * proximity
gen MS              	= dep_as * dep_dm
gen MSb            		= dep_as_1tier * dep_dm_1tier
gen lnms 				= log(MS)
gen lnmsb 				= log(MSb)
gen eth_ms 				= ethnic * lnms
gen eth_msb  			= ethnic * lnmsb
gen lnethnic 			= ln(ethnic)
gen lneth_lnmsb 		= lnethnic * lnmsb 
gen Nvo_simple 			= [(MS)^0.25 + 1]
gen lnNvo_simple 		= ln(Nvo_simple)
gen lneth_lnNvo  		= lnethnic * lnNvo_simple       
gen lnenps 				= ln(enps_bn)
gen lnenpv 				= ln(enpv_bn)
gen formula_proximity	= pres_formula*proximity 
gen Ns_simple			= MS^0.167
gen Ns_complex			= MSb^0.167
gen Nv_simple           = [((MS)^(1/4)) + 1]^(2/3)
gen lnNvPredicted 		= ln(Nv_simple)
gen Nv_complex          = ((4^upper)*((MSb)^1/4) + 1)^(2/3)
gen Nv_complex_BN       = [((1.7^upper)*((MSb)^(1/4))) + 1]^(2/3)

*******************************************************************************************************************************************
// Table 2 Electoral Experience, Segmented Samples				 
*******************************************************************************************************************************************

//  All elections 									  	<---------- From TABLE 2 Model 1
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time, cluster(country_code)
eststo model1

//  Early elections								 		<---------- From TABLE 2 Model 2
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number < 6, cluster(country_code)
eststo model2
 
// Late elections									 	<---------- From TABLE 2 Model 3
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number > 5, cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)

*******************************************************************************************************************************************
// Figure 1: The Marginal Effect of Ethnic Heterogeneity on the ENPv by Average District Magnitude (From TABLE 2 Model 3)						 
*******************************************************************************************************************************************

preserve
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number > 5, cluster(country_code)
generate JH=((_n-1)/10)
replace JH =. if _n>60
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar covb2b3=V[2,3]
set more off
gen conb=b1+b3*JH
set more off
gen conse=sqrt(varb1+varb3*JH^2+2*covb1b3*JH)
set more off
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb JH, clwidth(medium) clcolor(blue) clcolor(black)) (line top  JH, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom JH, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties", size(small)) subtitle("Pooled Analysis: Latin America Late Elections", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
graph export "Figure 1.pdf", replace
restore

*******************************************************************************************************************************************
// Table 3 Electoral Experience, Full Sample								 
*******************************************************************************************************************************************

reg enpv_bn ethnic maglog number upper enc_bn proximity time eth_maglog eth_upper enc_prox eth_number maglog_number eth_maglog_number, cluster(country_code)
eststo model1

esttab model1, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01) 

*******************************************************************************************************************************************
// Figure 2: The Marginal Effect of Ethnic Heterogeneity on the ENPv by Average District Magnitude when Number = 2	(from Table 3)							 
*******************************************************************************************************************************************

preserve
reg enpv_bn ethnic maglog number eth_maglog maglog_number eth_number eth_maglog_number upper eth_upper proximity enc_bn enc_prox time, cluster(country_code)
matrix list e(b)
matrix list e(V)
gen z =((_n-1)/10)
replace z =. if _n>60
gen conb=(4.94264+-3.00981*z+-0.21208*2+0.9566*z*2)
gen conse=sqrt(36.76387+(z^2)*6.600833+((2)^2)*0.03157284+(z^2)*((2)^2)*0.2640997+2*z*-14.325346625+2*2*0.252782+2*z*2*-0.553226+2*z*2*-0.1301509+2*2*(z^2)*0.220163+2*((2)^2)*z*-0.07254412)
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb z, clwidth(medium) clcolor(blue) clcolor(black)) (line top  z, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom z, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Number=2", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
restore

*******************************************************************************************************************************************
// Figure 3	: The Marginal Effect of Ethnic Heterogeneity on the ENPv by Average District Magnitude when Number = 9 (from Table 3)					 
*******************************************************************************************************************************************

preserve
reg enpv_bn ethnic maglog number eth_maglog maglog_number eth_number eth_maglog_number upper eth_upper proximity enc_bn enc_prox time, cluster(country_code)
matrix list e(b)
matrix list e(V)
gen z =((_n-1)/10)
replace z =. if _n>60
gen conb=(4.94264+-3.00981*z+-0.21208*9+0.9566*z*9)
gen conse=sqrt(36.76387+(z^2)*6.600833+((9)^2)*0.03157284+(z^2)*((9)^2)*0.2640997+2*z*-14.325346625+2*9*0.252782+2*z*9*-0.553226+2*z*9*-0.1301509+2*9*(z^2)*0.220163+2*((9)^2)*z*-0.07254412)
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb z, clwidth(medium) clcolor(blue) clcolor(black)) (line top z, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom z, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Number=9", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
restore

*******************************************************************************************************************************************
// Table 4 Electoral Experience and Electoral Regime Stability, Segmented Samples 							 
*******************************************************************************************************************************************

//  All elections 									  <---------- From TABLE 4 Model 1
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if stability >=3 , cluster(country_code)
eststo model1

//  Early elections								 <---------- From TABLE 4 Model 2
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number < 6 & stability >=3 , cluster(country_code)
eststo model2
 
// Late elections									 <---------- From TABLE 4 Model 3
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number > 5 & stability >=3, cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)

*******************************************************************************************************************************************
// Figure 4: The Marginal Effect of Ethnic Heterogeneity on the ENPv by Average District Magnitude (From Table 4 Model 3)								 
*******************************************************************************************************************************************

preserve
reg enpv_bn ethnic maglog eth_maglog upper enc_bn proximity eth_upper enc_prox time if number > 5 & stability >=3, cluster(country_code)
generate JH=((_n-1)/10)
replace JH =. if _n>60
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar covb2b3=V[2,3]
set more off
gen conb=b1+b3*JH
set more off
gen conse=sqrt(varb1+varb3*JH^2+2*covb1b3*JH)
set more off
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb JH, clwidth(medium) clcolor(blue) clcolor(black)) (line top  JH, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom JH, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties", size(small)) subtitle("Pooled Analysis: Latin America Late Elections", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
graph export "Figure 4.pdf", replace
restore

*******************************************************************************************************************************************
// Figure 5 Marginal effect of proximate presidential elections on the ENPv by ENC  (From Table 4 Model 3)  
*******************************************************************************************************************************************

preserve
reg enpv_bn proximity enc_bn enc_prox ethnic maglog eth_maglog upper eth_upper time if number > 5 & stability >=3, robust cluster(country_code)
generate JH=((_n-1)/10)
replace JH =. if _n>100
matrix b=e(b)
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar covb2b3=V[2,3]
set more off
gen conb=b1+b3*JH
set more off
gen conse=sqrt(varb1+varb3*JH^2+2*covb1b3*JH)
set more off
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb JH, clwidth(medium) clcolor(blue) clcolor(black)) (line top  JH, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom JH, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)10, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Proximate Presidential Elections on the Effective Number of Electoral Parties", size(small)) subtitle("Pooled Analysis: Latin America Late Elections", size(vsmall) margin(b=4)) xtitle("Effective Number of Presidential Candidates", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Presidential Elections", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
graph export "Figure 5.pdf", replace
restore

*******************************************************************************************************************************************
// Table 5 Electoral Regime Stability, Full Sample								 
*******************************************************************************************************************************************

reg enpv_bn ethnic maglog stability upper enc_bn proximity time eth_maglog eth_upper enc_prox eth_stability maglog_stability eth_maglog_stability, cluster(country_code)
eststo model1

esttab model1, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01) 

*************************************************************************************************************************************************
// Figure 6  Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Stability=2
*************************************************************************************************************************************************

preserve
reg enpv_bn ethnic maglog stability eth_maglog maglog_stability eth_stability eth_maglog_stability upper eth_upper proximity enc_bn enc_prox time, cluster(country_code)
matrix list e(b)
matrix list e(V)
gen z =((_n-1)/10)
replace z =. if _n>60
gen conb=(3.657932+-3.402626*z+-0.0679031*2+1.090463*z*2)
gen conse=sqrt(19.81706+(z^2)*3.248082+((2)^2)*0.02494463+(z^2)*((2)^2)*0.226096+2*z*-7.627942+2*2*0.204798+2*z*2*-0.04678393+2*z*2*-0.08252217+2*2*(z^2)*-0.0732499+2*((2)^2)*z*-0.05252667)
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb z, clwidth(medium) clcolor(blue) clcolor(black)) (line top  z, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom z, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Stability=2", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
restore

************************************************************************************************************************************************
// Figure 7 Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Stability=9
************************************************************************************************************************************************

** see R script for proof.
preserve
matrix list e(b)
matrix list e(V)
reg enpv_bn ethnic maglog stability eth_maglog maglog_stability eth_stability eth_maglog_stability upper eth_upper proximity enc_bn enc_prox time, cluster(country_code)
gen z =((_n-1)/10)
replace z =. if _n>60
gen conb=(3.657932+-3.402626*z+-0.0679031*9+1.090463*z*9)
gen conse=sqrt(19.81706+(z^2)*3.248082+((9)^2)*0.02494463+(z^2)*((9)^2)*0.226096+2*z*-7.627942+2*9*0.204798+2*z*9*-0.04678393+2*z*9*-0.08252217+2*9*(z^2)*-0.0732499+2*((9)^2)*z*-0.05252667)
gen a=1.684*conse
gen top=conb+a
gen bottom=conb-a
set textsize 100
graph twoway (line conb z, clwidth(medium) clcolor(blue) clcolor(black)) (line top  z, clpattern(dash) clwidth(thin) clcolor(black)) (line bottom z, clpattern(dash) clwidth(thin) clcolor(black)), xlabel(0(1)5, labsize(2.5)) ylabel(-15(5)15, nogrid labsize(2.5)) legend(col(2) order(1 2) label(1 "Marginal Effect") label(2 "90% Confidence Interval") label(3 " ") size(vsmall)) yline(0, lcolor(black)) title("The Marginal Effect of Ethnic Heterogeneity on the Effective Number of Electoral Parties by Average District Magnitude When Stability=9", size(vsmall) margin(b=4)) xtitle("Log of Average District Magnitude", size(small)) xsca(titlegap(2)) ysca(titlegap(2)) ytitle("Marginal Effect of Ethnic Heterogeneity", size(small)) scheme(s2mono)  plotregion(lcolor(black) lstyle(solid) lwidth(thin thin)) graphregion(color(white))
restore

********************************************************************************************************************************************************************************
 ****** Online Appendix ******
******************************************************************************************************************************************************************************** 


*****************************************************
*Table A1. Institutional Model (Electoral Experience)
*****************************************************

//  All elections 									  <---------- From TABLE A1 Model 1 
reg enpv_bn lnmsb upper  pres_formula proximity formula_proximity time, robust cluster(country_code)
eststo model1

//   Early elections 									  <---------- From TABLE A1 Model 2  
reg enpv_bn lnmsb upper pres_formula proximity formula_proximity time if number < 6, robust cluster(country_code)
eststo model2 

// Late elections								     <---------- From TABLE A1 Model 3 
reg enpv_bn lnmsb upper pres_formula proximity formula_proximity time if number > 5, robust cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)  

***************************************************** 
*Table A2. Predicted ENPv (Electoral Experience)
*****************************************************

// Simple Systems 
// All elections  <---------- From TABLE A2 Model 1	
reg enpv_bn Nv_simple if upper==0,  cluster(country_code)
eststo model1

//  Early elections <---------- From TABLE A2 Model 2
reg enpv_bn Nv_simple if upper==0 & number < 6 ,  cluster(country_code)
eststo model2 

//  Late elections  <---------- From TABLE A2 Model 3	
reg enpv_bn Nv_simple if upper==0 & number > 5, cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01) 

// Complex Systems 
// All elections   <---------- From TABLE A2 Model 4
reg enpv_bn Nv_complex_BN, robust cluster(country_code)  
eststo model4 

//  Early elections  <---------- From TABLE A2 Model 5
reg enpv_bn Nv_complex_BN if number < 6 ,  cluster(country_code)   
eststo model5 

//  Late elections  <---------- From TABLE A2 Model 6
reg enpv_bn Nv_complex_BN if number > 5 , cluster(country_code)
eststo model6

esttab model4 model5 model6, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01) 

*********************************************************************
*Table A3. Institutional Model (Electoral Experience and Stability)
*********************************************************************

//  All elections                                         <---------- From TABLE A3 Model 1	
reg enpv_bn lnmsb upper pres_formula proximity formula_proximity time if dep_elec_acc>=3, robust cluster(country_code)
eststo model1

// Early elections                                       <---------- From TABLE A3 Model 2
reg enpv_bn lnmsb   upper pres_formula proximity formula_proximity time if number < 6 & dep_elec_acc>=3, robust cluster(country_code)
eststo model2 

//  Late elections                                       <---------- From TABLE A3 Model 3
reg enpv_bn lnmsb upper pres_formula proximity formula_proximity time if number > 5 & dep_elec_acc>=3, robust cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)  

*********************************************************************  
*Table A4. Predicted ENPv (Electoral Experience and Stability)
*********************************************************************  

// Simple Systems 
//  All elections                                         <---------- From TABLE A4 Model 1	
reg enpv_bn Nv_simple if upper==0 & dep_elec_acc>=3, robust cluster(country_code)
eststo model1

//  Early elections                                        <---------- From TABLE A4 Model 2
reg enpv_bn Nv_simple if upper==0 & number < 6 & dep_elec_acc>=3, robust cluster(country_code)
eststo model2 

//  Late elections                                         <---------- From TABLE A4 Model 3	
reg enpv_bn Nv_simple if upper==0 & number > 5 & dep_elec_acc>=3, robust cluster(country_code)
eststo model3

esttab model1 model2 model3, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)  

// Complex Systems                                         <---------- From TABLE A4 Model 4
reg enpv_bn Nv_complex_BN if dep_elec_acc>=3, robust cluster(country_code)
eststo model4

//  Early elections                                        <---------- From TABLE A4 Model 5
reg enpv_bn Nv_complex_BN if number < 6 & dep_elec_acc>=3, robust cluster(country_code)
eststo model5 

//  Late elections                                         <---------- From TABLE A4 Model 6
reg enpv_bn Nv_complex_BN if number > 5 & dep_elec_acc>=3, robust cluster(country_code)
eststo model6

esttab model4 model5 model6, r2 ar2 se star(* 0.10 ** 0.05 *** 0.01)  

*********************************************************************  
* Figure A1
*********************************************************************  

** variations of Figure 2 and Figure 3 (replace values 1-12)

*********************************************************************  
* Figure A2
*********************************************************************  

** variations of Figure 6 and Figure 7 (replace values 1-12)

*********************************************************************  
*END
*********************************************************************  
